<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="i-ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon style="fontsize: 12px" icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <StoreTagDrawer @register="registerDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="shop-store-tag" setup>
  import { ref, onMounted, unref } from "vue"
  import { Icon } from "/@/components/Icon"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import StoreTagDrawer from "./StoreTagDrawer.vue"
  import { useMethods } from "/@/hooks/system/useMethods"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./storeTag.data"
  import { listStoreTag, deleteStoreTag, batchDeleteStoreTag } from "/@/api/misc/store"
  import { useListPage } from "/@/hooks/system/useListPage"

  const tenantId = ref(null)
  const glob = useGlobSetting()
  const [registerDrawer, { openDrawer }] = useDrawer()

  const { prefixCls, tableContext } = useListPage({
    designScope: "article-group",
    tableProps: {
      title: "门店标签",
      api: listStoreTag,
      bordered: false,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
        showResetButton: false,
      },
    },
  })

  const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext

  onMounted(() => {
    const { updateSchema } = getForm()
  })

  function handleAdd() {
    openDrawer(true, {
      isUpdate: false,
    })
  }

  function handleEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
    })
  }

  async function handleDelete(record) {
    await deleteStoreTag({ id: record.id }, reload)
  }

  async function batchHandleDelete() {
    await batchDeleteStoreTag({ ids: selectedRowKeys.value }, reload)
  }

  function getActions(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
        // ifShow: record.sendStatus == 0
      },
      {
        label: "删除",
        ifShow: record.sendStatus != 1,
        color: 'error',
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
</script>
